var util = require('../../libs/util.js');
var moment = require('../../libs/moment.min.js');
moment.locale('zh-cn');
var app = getApp()
Page({
  data: {
    hasData: false,
    order_state: 0,
    order_list: [{
      ID: "",
      warehouse: "",
      total_package: "",
      total_cbm: "",
      total_weight: "",
      order_state: "",
      ADD_TIME: ""
    }],
    order_flow_btn: true,
    tab: 1,
    searchShowed: false,
    searchVal: "",
    selected: false,
    pageSize: 10,
    pageNumber:0,
    count:0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function () {
    wx.setNavigationBarTitle({ "title": app.appName +"--司机服务" });
    wx.showToast({
      title: "数据加载中，请稍候...",
      icon: 'loading',
      duration: 3000
    });
    this.loadData();
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.setData({
      currentPage: 1
    });
    this.loadData();
  },
  loadData: function () {
    var that = this;
    let condition = " a.driver='"+app.globalData.driverId+"' and a.driver_status = 'end' "
    util.postData("/zhc/datalist/getDataByListId", {
      "list_id": "E323E43B-E768-407E-A067-6DF0DBD079CF",
      "page": {
        "size": 50,
        "current": 1,
        "orders": [{
          "column": "ADD_TIME",
          "asc": "false"
        }]
      },
      "data": {
        "field": "a.path_name,a.state,a.otherfeeName,a.total_price,a.containeryard,a.wh_time,a.delivery_sort,a.driverPrice,a.deliveryThirdParty_income,a.latitude_current,a.delivery_time,a.vehicleSource,a.onePrice,a.tp_time,a.deliveryPrice,a.unload_box,a.month_knot_income,a.mileage,a.configExplain,a.deliveryThirdParty_pay,a.ADD_USER,a.remoteGetVehicle,a.volume,a.parkingfee,a.delivery_mile,a.packages_expect,a.ctime,a.parkingfee_docs_compress,a.back_box,a.wh_remark,a.loading_rule,a.parkingfee_docs,a.ADD_USER_NAME,a.depart_time,a.wh_status,a.is_auction,a.cy_get,a.driverPrice2,a.onePrice_income,a.return_package,a.humidity_current,a.temperature_current,a.is_account_checking,a.total_package,a.delivery_point,a.cost2,a.deliveryOntime,a.deliveryEvaluation,a.vehicle_level,a.longitude_current,a.work_after_check,a.is_account_confirm,a.warehouse,a.driver,a.cost,a.work_before_check,a.custom_distance,a.loading_time,a.signin,a.auction_price_min,a.auction_price_max,a.total_weight,a.delivery_score,a.delivery_date,a.weight,a.ID,a.driver_status,a.delivery_picture,a.onePrice2,a.goodsValue,a.shipper_path,a.back_distance,a.income_pricemode,a.wh_user,a.deliveryMode,a.total_mileage,a.vehicleFunction,a.month_knot_pay,a.total_cbm,a.is_undertaking,a.tollfee_docs,a.specialPrice,a.customerName,a.cy_image,a.isReturn,a.receiving_order,a.vehicleLength,a.wh_attachment,a.tollfee,a.urgentGetVehicle,a.total_duration,a.franchiseDriverPrice,a.auction_vehicle_tags,a.is_close,a.driver_level,a.NO,a.is_cancel,a.returnBoxs,a.otherfee,a.packages,a.goodsname,a.remark,a.ADD_TIME,a.vehicle,a.back_duration,a.auction_param,a.create_mode,a.BE_DEL"
      },
      "repeat_field": [],
      "condition": condition,
      "virtual_field": [{
          "relate": "LEFT JOIN",
          "res": "47bb1f0ec0874fb5961186f86b97792e",
          "alias": "b",
          "on": "a.vehicle=b.ID",
          "type": "",
          "condition": "",
          "from_field": "plate",
          "field_name": "plate",
          "field_alias": "车牌"
        },
        {
          "relate": "LEFT JOIN",
          "res": "88e65a5ccd734a35a0cac74c9d58ff2e",
          "alias": "c",
          "on": "a.driver=c.ID",
          "type": "",
          "condition": "",
          "from_field": "name",
          "field_name": "driver_name",
          "field_alias": "司机"
        },
        {
          "relate": "LEFT JOIN",
          "res": "755fb921d96f4db59773592b345fd7e1",
          "alias": "d",
          "on": "a.ID=d.delivery",
          "type": "COUNT",
          "condition": "",
          "from_field": "ID",
          "field_name": "shop_number",
          "field_alias": "配送点数量"
        }
      ]
    }, function (res, error) {
      console.log(res);
      console.log(error)
      wx.hideLoading();
      if (res.data.success && res.data?.result?.records.length > 0) {
          that.setData({
            hasData: true
          });
          var data = res.data.result.records, _data = [];
          for (var i = 0; i < data.length; i++) {
            _data.push({
              ID: data[i].ID,
              NO: data[i].NO,
              ADD_TIME: moment(data[i].ADD_TIME).format("YYYY年MM月DD日 hh时"),
              path_name: data[i].path_name,
              wh_name: data[i].wh_name,
              wh_address: data[i].wh_address,
              shop_number: data[i].shop_number,
              total_mileage: data[i].total_mileage,
              total_duration: data[i].total_duration,
              packages_expect: data[i].packages_expect,
              unit: data[i].unit,
              driverPrice: data[i].driverPrice,
              order_state: data[i].state
            });
            that.setData({
              order_state: data[i].state
            });
          };
        var count = res.data.result.total;
        var pageNumber = Math.ceil(count / that.data.pageSize);
          that.setData({
            order_list: _data,
            count: count,
            pageNumber: pageNumber,
            page: that.data.page == 0 ? 1 : that.data.page
          });
          that.setPageStatus();

          if (that.data.order_state == "0" || that.data.order_state == "2" || that.data.order_state == "4") {
            that.setData({
              order_flow_btn: false
            });
          }
        } else {
          that.setData({
            hasData: false,
            page: 0,
            order_list: []
          })
        };
    })
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  gotoScan: function () {
    util.driverScan();
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/driver/orderList'
    })
  },
  gotoDriverInfo: function () {
    wx.redirectTo({
      url: '/pages/driver/driverInfo'
    })
  },
  gotoCurrentOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/currentOrder'
    })
  },
  gotoIotBox: function () {
    wx.redirectTo({
      url: '/pages/driver/iotBox'
    })
  },
  gotoHistoryOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/historyOrder'
    })
  }
})
